package com.daylightrobbery.server.packet.game;


import com.daylightrobbery.server.GameManager;
import com.daylightrobbery.server.entity.PlayerInfo;
import com.daylightrobbery.server.entity.Room;
import com.daylightrobbery.server.packet.game.common.BlockData;
import com.daylightrobbery.server.packet.game.common.DataReader;
import com.daylightrobbery.server.packet.gameobjects.HouseDescription;
import com.daylightrobbery.server.packet.type.DataPacket;
import com.daylightrobbery.server.packet.type.RoomDataPacket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

@Component
public class StartGamePacket implements RoomDataPacket {

    private static final Logger log = LoggerFactory.getLogger(StartGamePacket.class);

    private static final Integer INIT_MONEY = 14000;
    private static final Integer INIT_TICKETS = 100;
    private static final Integer REWARD_TICKET = 20; // 每次奖励的数量

    private String uid;
    private Integer roomId;
    private Integer pathCounts;
    private int type = 30001;

    @Override
    public DataPacket process() {
        log.info("Start Game...");
        GamePrepareDataPacket packet = new GamePrepareDataPacket();
        packet.setRoomId(roomId);
        packet.setCurrentTurnPlayerUid(uid);
        packet.setRewardTickets(REWARD_TICKET);
        Room room = GameManager.getInstance().getRoomById(roomId);
        room.setStatus(true);
        ConcurrentHashMap<String, PlayerInfo> players =
                room.getPlayers();
        packet.setPathCounts(64);
        int index = 0;
        for (Map.Entry<String, PlayerInfo> playerInfoEntry : players.entrySet()) {
            PlayerInfo playerInfo = playerInfoEntry.getValue();
            packet.addPlayerData(
                    playerInfoEntry.getKey(),
                    new PlayerInitAssets().setMoney(INIT_MONEY).setTickets(INIT_TICKETS)
            );  // 添加游戏资产数据
            packet.addCharacterInfo(playerInfo.getUid(), index++); // 添加人物资源信息
            //packet.addPlayerPosition(playerInfo.getUid(), ThreadLocalRandom.current().nextInt(0, 50));
            packet.addPlayerPosition(playerInfo.getUid(), 0);
            packet.addPlayerInfo(playerInfo.getUid(), playerInfo);
            GameManager.getInstance().addPlayerInGame(playerInfo.getUid(), roomId);
        }
        HashMap<Integer, BlockData> blockDataMap = DataReader.read();
        pathCounts = blockDataMap.size();
        // 初始化地皮
        for (Map.Entry<Integer, BlockData> dataEntry : blockDataMap.entrySet()) {
            HouseDescription houseDesc = new HouseDescription();
            BlockData data = dataEntry.getValue();
            houseDesc
                    .setUid(null)
                    .setLevel(0)
                    .setBlockType(data.getBlockType())
                    .setLandPrice(data.getMoney())
                    .setChineseName(data.getChineseName())
                    .setEnglishName(data.getEnglishName())
                    .setLevel0Price(data.getLevel0Price())
                    .setLevel1Price(data.getLevel1Price())
                    .setLevel2Price(data.getLevel2Price())
                    .setLevel3Price(data.getLevel3Price())
                    .setLevel4Price(data.getLevel4Price())
                    .setLevel5Price(data.getLevel5Price())
                    .setLevelUpPrice(data.getLevelUpPrice())
                    .setMortgagePrice(data.getMortgagePrice())
                    .setAscription(data.getAscription());
            packet.addHouseInfo(dataEntry.getKey(), houseDesc);
        }

        // 设置地砖数量
        packet.setPathCounts(pathCounts);
        room.setPacket(packet);
        return packet;
    }

    @Override
    public int getType() {
        return type;
    }

    public String getUid() {
        return uid;
    }

    public StartGamePacket setUid(String uid) {
        this.uid = uid;
        return this;
    }

    public int getRoomId() {
        return roomId;
    }

    public StartGamePacket setRoomId(Integer roomId) {
        this.roomId = roomId;
        return this;
    }

    public Integer getPathCounts() {
        return pathCounts;
    }

    public StartGamePacket setPathCounts(Integer pathCounts) {
        this.pathCounts = pathCounts;
        return this;
    }

    @Override
    public String toString() {
        return "CreateRoomPacket{" +
                "uid='" + uid + '\'' +
                '}';
    }

}
